package com.example.sfm.controller.admin;

import com.example.sfm.constant.MessageConstants;
import com.example.sfm.pojo.dto.UserCreateDto;
import com.example.sfm.pojo.dto.UserQueryDto;
import com.example.sfm.pojo.dto.UserUpdateDto;
import com.example.sfm.result.Result;
import com.example.sfm.service.IUsersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sfm
 * @since 2025-03-07
 */
@RestController("adminUsersController")
@RequestMapping("/admin/users")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "用户管理接口", description = "提供用户信息的增删改查、重置密码、导出等操作")
public class UsersController {
    private final IUsersService userService;

    /**
     * 创建用户
     */
    @PostMapping("/create")
    @Operation(summary = "创建用户", description = "根据传入的用户信息创建新用户")
    public Result<?> createUser(@RequestBody @Validated @NotNull @Parameter(description = "用户信息") UserCreateDto dto) {
        log.info("创建用户: {}", dto.getUsername());
        userService.createUser(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    /**
     * 分页查询用户
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询用户", description = "根据查询条件分页查询用户信息")
    public Result<?> listUsers(@ParameterObject UserQueryDto dto) {
        log.info("查询用户列表: {}", dto);
        return Result.success(userService.listUsers(dto), MessageConstants.SELECT_OK);
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/update/{id}")
    @Operation(summary = "更新用户信息", description = "根据用户ID更新用户的相关信息")
    public Result<?> updateUser(@Parameter(description = "用户ID",in = ParameterIn.PATH) @PathVariable Integer id,
                                @RequestBody @Validated @Parameter(description = "更新内容") UserUpdateDto dto) {
        log.info("更新用户: ID={}", id);
        userService.updateUser(id, dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 删除用户（仅管理员）
     */
    @DeleteMapping("/delete")
    @Operation(summary = "批量删除用户", description = "根据传入的用户ID列表批量删除用户信息，仅管理员可操作")
    public Result<?> deleteUsers(@Parameter(description = "用户ID列表",in = ParameterIn.QUERY) @RequestParam List<Integer> ids) {
        log.info("批量删除用户: IDs={}", ids);
        userService.deleteUsers(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 重置密码（仅管理员）
     */
    @PutMapping("/reset-password/{id}")
    @Operation(summary = "重置用户密码", description = "根据用户ID重置用户密码，仅管理员可操作")
    public Result<?> resetPassword(@Parameter(description = "用户ID",in = ParameterIn.PATH) @PathVariable Integer id) {
        userService.resetPassword(id);
        return Result.success("密码重置成功");
    }

    @GetMapping("/export")
    @Operation(summary = "导出用户信息", description = "根据查询条件导出用户信息到Excel文件")
    public void exportExcel(HttpServletResponse response, @ParameterObject UserQueryDto dto) {
        log.info("导出用户");
        userService.exportUsers(response, dto);
    }
}